package com.laijiale.mapper;

import com.laijiale.entities.Room;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author JoeZhou
 */
@Repository
public interface RoomMapper {

    @Insert("insert into rms_room " +
            "(`title`, `address`, `info`, `capacity`, " +
            "`deleted`, `created`, `updated`) " +
            "values (#{title}, #{address}, #{info}, #{capacity}, " +
            "#{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Room room);

    @Select("select t.id, t.title, t.address, t.info, t.capacity, " +
            "t.deleted, t.created, t.updated from rms_room t " +
            "where t.id = #{param1} and t.deleted = 0")
    Room selectById(Long id);

    @Update("<script>" +
            "update rms_room " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if> " +
            "<if test='address != null'> address = #{address}, </if> " +
            "<if test='info != null'> info = #{info}, </if> " +
            "<if test='capacity != null'> capacity = #{capacity}, </if> " +
            "<if test='deleted != null'> deleted = #{deleted}, </if> " +
            "<if test='created != null'> created = #{created}, </if> " +
            "<if test='updated != null'> updated = #{updated}, </if> " +
            "</set> " +
            "where id = #{id} and deleted = 0" +
            "</script>")
    int updateById(Room room);

    @Update("update rms_room set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update rms_room set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select t.id, t.title, t.address, t.info, t.capacity, " +
            "t.deleted, t.created, t.updated from rms_room t " +
            "where t.deleted = 0")
    List<Room> list();

    @Delete("delete from rms_room where id = #{param1}")
    int deleteById(Long id);

    @Select("select t.id, t.title, t.address, t.info, t.capacity, " +
            "t.deleted, t.created, t.updated from rms_room t " +
            "where t.deleted = 0 and t.title like concat('%', #{param1}, '%')")
    List<Room> listLikeTitle(String title);

}
